﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Management;
using System.Text;
using UFC.Common;
using System.Configuration;

namespace IISAutoStart
{
    public partial class IISAutoStartService : ServiceBase
    {
        private System.Timers.Timer m_SyncTimer;
        public IISAutoStartService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            LogHelp.SetConfig();
            int intSync =Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());
            SyncTimerStatr(ref m_SyncTimer, m_SyncTimer_Elapsed, intSync * 1000);
        }
        /// <summary>
        /// 传参启动新线程并运行方法
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="m_timerElapsed"></param>
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync;
            timer.Enabled = true;
            timer.Start();
        }
        private void m_SyncTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SyncTimer.Enabled = false;
                //LogHelp.WriteLog("IISAutoStartService 服务启动");
                AutoStartApplicationPool aap = new AutoStartApplicationPool();
                aap.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError(myError.Message, myError);
            }
            finally
            {
                m_SyncTimer.Enabled = true;
            }
        }
        protected override void OnStop()
        {
        }
    }
}
